package day8;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.Test;



public class ArrayListDemo {
	
	@Test
	public void test1() {
		int[] array = new int[10];
		Student[] array2 = new Student[10];
		// 为了解决数组长度固定问题, 读写都是通过下标，ArrayList对数组的封装
		// Object是所有类的父类，所以可以吧任何的对象放到这个集合里面
		// Object[] elementData;
		// new出来的子类对象可以赋给父类的引用
		ArrayList list1 = new ArrayList();
		list1.add(1);
		list1.add("abc");
		// 数组是存放相同类型数据的集合 
		// <Student>泛型：这个ArrayList里面只能放Student
		ArrayList<Student> list = new ArrayList<Student>();
		Student student1 = new Student(1, "张三", 23, "男");
		Student student2 = new Student(2, "李四", 23, "男");
		Student student3 = new Student(3, "王五", 23, "男");
		// 在添加时候不需要关心下标
		list.add(student1);
		list.add(student2);
		list.add(student3);
		// list.add("abc");
		
		// ArrayList遍历方式一：
		for (int i = 0; i < list.size(); i++) {
			// 等价于原来：数组名[下标]
			Student student = list.get(i);
			System.out.println(student);
		}
		
		// ArrayList遍历方式二：foreach
		for (Student student : list) {
			System.out.println(student);
		}
	}
	
	@Test
	public void test2() {
		ArrayList<Student> list = new ArrayList<Student>();
		Student student1 = new Student(1, "张三", 23, "男");
		Student student2 = new Student(2, "李四", 23, "男");
		Student student3 = new Student(3, "王五", 23, "男");
		list.add(student1);
		list.add(student1);
		list.add(student2);
		list.add(student3);
		list.remove(student3);
		System.out.println(list.size());
		for (Student student : list) {
			System.out.println(student);
		}
		System.out.println("-----------------------");
		
		HashSet<Student> set = new HashSet<Student>();
		set.add(student1);
		set.add(student1);
		set.add(student2);
		set.add(student3);
		System.out.println(set.size());
		for (Student student : set) {
			System.out.println(student);
		}
	}
	
	@Test
	public void test3() {
//		HashMap<String, Object> map = new HashMap<String, Object>();
		Map<String, String> map = new HashMap<String, String>();// Object
		map.put("CN", "中国人民共和国");
		map.put("US", "美利坚合众国");
		String value1 = map.get("CN");// (String)
		System.out.println(value1);
		
		// Map的遍历
		Set<Entry<String, String>> set = map.entrySet();
		for (Entry<String, String> entry : set) {
			String key = entry.getKey();
			String value = (String)entry.getValue();
			System.out.println(key + ":" + value);
		}
		System.out.println("----------------------------");
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			String value = map.get(key);
			System.out.println(key + ":" + value);
		}
		
		String java = "Java";
		System.out.println(java.hashCode());
		int[] i = {1,5};
		System.out.println(i.hashCode());
	}
	
}
